#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<windows.h>
#define ROWS_A 5
#define COLS_A 4
#define ROWS_B 4
#define COLS_B 2

//计算实现过程
void matrix_real_multiply(double matrix_A[][COLS_A], double matrix_B[][COLS_B], double matrix_C[][COLS_B]){
    for (int i = 0; i < ROWS_A; i++){
        for (int j = 0; j < COLS_B; j++){  
            matrix_C[i][j] = 0;
            for (int k = 0; k < COLS_A; k++){  
                matrix_C[i][j] += matrix_A[i][k] * matrix_B[k][j];
            }
        }
    }
}
void matrix_complex_multiply(double matrix_realA[][COLS_A],double matrix_imaginaryA[][COLS_A],double matrix_realB[][COLS_B],double matrix_imaginaryB[][COLS_B],double matrix_realC[][COLS_B],double matrix_imaginaryC[][COLS_B]){
	for(int i = 0; i < ROWS_A; ++i){
		for(int j = 0; j < COLS_A; ++j){
			matrix_realC[i][j] = 0;matrix_imaginaryC[i][j] = 0;
			for(int k = 0; k < COLS_B; ++k){
				matrix_realC[i][k]+=(matrix_realA[i][k] * matrix_realB[k][j] - matrix_imaginaryA[i][k] * matrix_imaginaryB[k][j]);
				matrix_imaginaryC[i][k]+=(matrix_realA[i][k] * matrix_imaginaryB[k][j] + matrix_realB[k][j] * matrix_imaginaryA[i][k]);
			}
		}
	}
}
int main()
{
	srand((unsigned int)time(NULL));
    double matrix_A[ROWS_A][COLS_A];
    double matrix_B[ROWS_B][COLS_B];
    double matrix_C[ROWS_A][COLS_B]; //乘积得到的矩阵C
    double matrix_realA[ROWS_A][COLS_A];
    double matrix_realB[ROWS_B][COLS_B];
    double matrix_realC[ROWS_A][COLS_B];//乘积得到的矩阵C实部
    double matrix_imaginaryA[ROWS_A][COLS_A];
    double matrix_imaginaryB[ROWS_B][COLS_B];
    double matrix_imaginaryC[ROWS_A][COLS_B];//乘积得到的矩阵C虚部
    //赋值
    for (int i = 0; i < ROWS_A; i++){
	        for (int j = 0; j < COLS_A; j++){
	            matrix_A[i][j] = (double)rand()/RAND_MAX*100-50;
	        }
	    }
	for (int i = 0; i < ROWS_B; i++){
	        for (int j = 0; j < COLS_B; j++){
	            matrix_B[i][j] = (double)rand()/RAND_MAX*100-50;
	        }
	    }
	for (int i = 0; i < ROWS_A; i++){
	        for (int j = 0; j < COLS_A; j++){
	            matrix_realA[i][j] = (double)rand()/RAND_MAX*100-50;
	        }
	    }
	for (int i = 0; i < ROWS_A; i++){
	        for (int j = 0; j < COLS_A; j++){
	            matrix_imaginaryA[i][j] = (double)rand()/RAND_MAX*100-50;
	        }
	    }
	for (int i = 0; i < ROWS_B; i++){
	        for (int j = 0; j < COLS_B; j++){
	            matrix_realB[i][j] = (double)rand()/RAND_MAX*100-50;
	        }
	    }
	for (int i = 0; i < ROWS_B; i++){
	        for (int j = 0; j < COLS_B; j++){
	            matrix_imaginaryB[i][j] = (double)rand()/RAND_MAX*100-50;
	        }
	    }
    matrix_real_multiply(matrix_A, matrix_B, matrix_C);
    matrix_complex_multiply(matrix_realA,matrix_imaginaryA,matrix_realB,matrix_imaginaryB,matrix_realC,matrix_imaginaryC);
    // 输出结果矩阵C
    printf("实数矩阵A：\n");
    for (int i = 0; i < ROWS_A; i++){
        for (int j = 0; j < COLS_A; j++){
            printf("%.2lf ", matrix_A[i][j]);  
        }
        printf("\n");
    }
    printf("实数矩阵B：\n");
    for (int i = 0; i < ROWS_B; i++){
        for (int j = 0; j < COLS_B; j++){
            printf("%.2lf ", matrix_B[i][j]);  
        }
        printf("\n");
    }
    printf("实数矩阵乘法：\n");
    for (int i = 0; i < ROWS_A; i++){
        for (int j = 0; j < COLS_B; j++){
            printf("%.2lf ", matrix_C[i][j]);  
        }
        printf("\n");
    }
    printf("复数矩阵A：\n");
    for (int i = 0; i < ROWS_A; i++){
        for (int j = 0; j < COLS_A; j++){
            printf("%.2lf+(%.2lf)i ", matrix_realA[i][j],matrix_imaginaryA[i][j]);  
        }
        printf("\n");
    } 
    printf("复数矩阵B：\n");
    for (int i = 0; i < ROWS_B; i++){
        for (int j = 0; j < COLS_B; j++){
            printf("%.2lf+(%.2lf)i ", matrix_realB[i][j],matrix_imaginaryB[i][j]);  
        }
        printf("\n");
    }
    printf("复数矩阵乘法：\n");
    for (int i = 0; i < ROWS_A; i++){
        for (int j = 0; j < COLS_B; j++){
            printf("%.2lf+(%.2lf)i ", matrix_realC[i][j],matrix_imaginaryC[i][j]);  
        }
        printf("\n");
    }
    return 0;
}
